<script setup lang="ts">
import { ref, onMounted, onUnmounted } from "vue";
import { getActiveQuestions } from "@/api/external";
import type { Question } from "@/types/question";

// 推荐问题列表
const questions = ref<Question[]>([]);
const loading = ref(false);
const error = ref("");
const activeIndex = ref(-1); // 当前高亮的问题索引
let hoverTimer: number | null = null;

const emit = defineEmits(["questionSelected"]);

// 获取推荐问题列表
const fetchQuestions = async () => {
  try {
    loading.value = true;
    error.value = "";
    const response = await getActiveQuestions();
    questions.value = response.questions;
    
    if (questions.value.length === 0) {
      error.value = "暂无推荐问题";
    }
  } catch (err) {
    console.error("获取推荐问题失败:", err);
    error.value = "获取推荐问题失败";
  } finally {
    loading.value = false;
  }
};

// 点击问题的处理函数
const handleQuestionClick = (question: Question) => {
  emit("questionSelected", question.title);
};

// 启动自动hover动效
const startAutoHover = () => {
  if (questions.value.length === 0) return;
  
  hoverTimer = window.setInterval(() => {
    activeIndex.value = (activeIndex.value + 1) % questions.value.length;
  }, 2500); // 每2.5秒切换一次
};

// 停止自动hover动效
const stopAutoHover = () => {
  if (hoverTimer !== null) {
    clearInterval(hoverTimer);
    hoverTimer = null;
  }
  activeIndex.value = -1;
};

onMounted(() => {
  fetchQuestions().then(() => {
    // 延迟1秒后开始自动hover效果
    setTimeout(() => {
      startAutoHover();
    }, 1000);
  });
});

onUnmounted(() => {
  stopAutoHover();
});
</script>

<template>
  <div class="recommended-questions">
    <div class="subhead">
      <div class="text">推荐问题</div>
    </div>
    
    <!-- 加载中状态 -->
    <div v-if="loading" class="loading-container">
      <div class="loading-spinner"></div>
      <div class="loading-text">加载中...</div>
    </div>
    
    <!-- 错误状态 -->
    <div v-else-if="error" class="error-container">
      <div class="error-icon">⚠</div>
      <div class="error-text">{{ error }}</div>
    </div>
    
    <!-- 问题列表 -->
    <div v-else class="questions-list">
      <div
        v-for="(question, index) in questions"
        :key="question.id"
        class="question-item"
        :class="{ 'is-active': index === activeIndex }"
        @click="handleQuestionClick(question)"
        @mouseenter="stopAutoHover"
        @mouseleave="startAutoHover"
      >
        <div class="question-icon">?</div>
        <div class="question-text">{{ question.title }}</div>
        <div class="question-count" v-if="question.usage_count > 0">
          {{ question.usage_count }}次
        </div>
      </div>
    </div>
  </div>
</template>

<style lang="scss" scoped>
.recommended-questions {
  .questions-list {
    margin-top: 20px;
    display: flex;
    flex-direction: column;
    gap: 12px;

    .question-item {
      width: 100%;
      min-height: 60px;
      background: linear-gradient(
        135deg,
        rgba(49, 190, 255, 0.08) 0%,
        rgba(49, 190, 255, 0.03) 100%
      );
      border: 1px solid rgba(49, 190, 255, 0.25);
      border-radius: 8px;
      padding: 12px 16px;
      display: flex;
      align-items: center;
      gap: 12px;
      cursor: pointer;
      transition: all 0.3s ease;
      backdrop-filter: blur(10px);

      &:hover,
      &.is-active {
        border-color: rgba(49, 190, 255, 0.5);
        background: linear-gradient(
          135deg,
          rgba(49, 190, 255, 0.15) 0%,
          rgba(49, 190, 255, 0.08) 100%
        );
        transform: translateX(-5px);
        box-shadow: 0 4px 12px rgba(49, 190, 255, 0.2);
      }

      &:active {
        transform: translateX(-5px) scale(0.98);
      }

      .question-icon {
        width: 32px;
        height: 32px;
        border-radius: 50%;
        background: linear-gradient(
          135deg,
          rgba(49, 190, 255, 0.3) 0%,
          rgba(49, 190, 255, 0.15) 100%
        );
        border: 1px solid rgba(49, 190, 255, 0.4);
        display: flex;
        align-items: center;
        justify-content: center;
        color: #31beff;
        font-size: 20px;
        font-weight: bold;
        flex-shrink: 0;
        transition: all 0.3s ease;
      }

      &:hover .question-icon,
      &.is-active .question-icon {
        background: linear-gradient(
          135deg,
          rgba(49, 190, 255, 0.5) 0%,
          rgba(49, 190, 255, 0.3) 100%
        );
        transform: rotate(15deg);
      }

      .question-text {
        flex: 1;
        color: #f6f9fe;
        font-family: "Alibaba PuHuiTi", "Microsoft YaHei", sans-serif;
        font-size: 15px;
        font-weight: 500;
        line-height: 1.5;
        opacity: 0.9;
        transition: all 0.3s ease;
      }

      &:hover .question-text,
      &.is-active .question-text {
        opacity: 1;
        color: #ffffff;
      }

      .question-count {
        font-size: 12px;
        color: rgba(49, 190, 255, 0.8);
        font-weight: 500;
        padding: 2px 8px;
        background: rgba(49, 190, 255, 0.15);
        border-radius: 10px;
        white-space: nowrap;
        flex-shrink: 0;
      }
    }
  }

  /* 加载和错误状态 */
  .loading-container,
  .error-container {
    margin-top: 20px;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 12px;
    padding: 40px 20px;
    background: linear-gradient(
      135deg,
      rgba(49, 190, 255, 0.08) 0%,
      rgba(49, 190, 255, 0.03) 100%
    );
    border: 1px solid rgba(49, 190, 255, 0.25);
    border-radius: 12px;
  }

  .loading-spinner {
    width: 40px;
    height: 40px;
    border: 3px solid rgba(49, 190, 255, 0.2);
    border-top-color: #31beff;
    border-radius: 50%;
    animation: spin 1s linear infinite;
  }

  @keyframes spin {
    to {
      transform: rotate(360deg);
    }
  }

  .loading-text,
  .error-text {
    color: rgba(246, 249, 254, 0.6);
    font-size: 14px;
    font-weight: 500;
  }

  .error-icon {
    font-size: 32px;
    color: rgba(255, 193, 7, 0.8);
  }

  .error-text {
    color: rgba(255, 193, 7, 0.8);
  }
}
</style>
